package com.example.zx.theline.lyjh.lyrjh.pub;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;
import com.example.zx.theline.lyjh.lyrjh.SgrjhActivity;
import com.example.zx.theline.lyjh.pub.JhshReadActivity;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.HashMap;
import java.util.Map;

import GsonBean.SgcxrjhDetailBean;
import GsonBean.ShyjhxqBean;
import GsonBean.SuccessBean;
import Utils.MyToast;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseActivity;
import dialog.CenterDialog;
import dialog.CustomDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Ren on 2017/9/18.
 * 日计划审核已完成界面点击详情进入页面
 */

public class SgrjhYwcXqActivity extends BaseActivity implements View.OnClickListener {


    //按钮
    private TextView leftTextView,centerTextView, cancel, ckyjh, zf, scanShlc;

    //计划内容
    private TextView lb, rjhh, ydbh,  kssj,xm, dd, hb, lc, xmmc, sgnr, sgjx, blfh, aqfx, fxcs;
    //相关单位
    private TextView sgdw, sgdwfzr, jldw, jldwfzr, jsdw, cwzd, ddt, dxjz, sbgldw, qtdw, shcs, xmzgbm;
    //Data
    private SgcxrjhDetailBean mDetailBean;
    private String bh;
    //日计划审核功能
    private CustomDialog customDialog;
    private CenterDialog mCenterDialog;
    private TextView cancelBtn, confirmBtn; //弹出框按钮
    private LinearLayout blfh_layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sgrjhywcxq);
        Intent intent = getIntent();
        bh = intent.getStringExtra("bh");
        InitView();
        InitListen();
        InitData();
    }

    @Override
    protected void InitView() {
        centerTextView = (TextView) findViewById(R.id.centerTextView);
        cancel = (TextView) findViewById(R.id.cancel);
        ckyjh = (TextView) findViewById(R.id.ckyjh);
        zf = (TextView) findViewById(R.id.zf);
        scanShlc = (TextView) findViewById(R.id.tv_scan_shlc);
        leftTextView = (TextView) findViewById(R.id.leftTextView);

        lb = (TextView) findViewById(R.id.lb);
        rjhh = (TextView) findViewById(R.id.rjhh);
        ydbh = (TextView) findViewById(R.id.ydbh);
        kssj = (TextView) findViewById(R.id.kssj);
        xm = (TextView) findViewById(R.id.xm);
        dd = (TextView) findViewById(R.id.dd);
        hb = (TextView) findViewById(R.id.hb);
        lc = (TextView) findViewById(R.id.lc);
        xmmc = (TextView) findViewById(R.id.xmmc);
        sgnr = (TextView) findViewById(R.id.sgnr);
        sgjx = (TextView) findViewById(R.id.sgjx);
        blfh = (TextView) findViewById(R.id.blfh);
        blfh_layout = (LinearLayout) findViewById(R.id.blfh_layout);
        aqfx = (TextView) findViewById(R.id.aqfx);
        fxcs = (TextView) findViewById(R.id.fxcs);

        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdwfzr = (TextView) findViewById(R.id.sgdwfzr);
        jldw = (TextView) findViewById(R.id.jldw);
        jldwfzr = (TextView) findViewById(R.id.jldwfzr);
        jsdw = (TextView) findViewById(R.id.jsdw);
        cwzd = (TextView) findViewById(R.id.cwzd);
        ddt = (TextView) findViewById(R.id.ddt);
        dxjz = (TextView) findViewById(R.id.dxjz);
        sbgldw = (TextView) findViewById(R.id.sbgldw);
        qtdw = (TextView) findViewById(R.id.qtdw);
        shcs = (TextView) findViewById(R.id.shcs);
        xmzgbm = (TextView) findViewById(R.id.xmzgbm);
        //审核的弹出框
        customDialog = new CustomDialog(this, R.layout.dialog_check_info, R.style.Theme_dialog);
        customDialog.setCancelable(false);
        cancelBtn = (TextView) customDialog.findViewById(R.id.btn_cancel);
        confirmBtn = (TextView) customDialog.findViewById(R.id.btn_confirm);
    }

    @Override
    protected void InitData() {
        centerTextView.setText("日计划详情");
        requestDetailData();
    }

    @Override
    protected void InitListen() {
        cancel.setOnClickListener(this);
        ckyjh.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        scanShlc.setOnClickListener(this);
        zf.setOnClickListener(this);
        leftTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.ckyjh: //查看月计划
                readMonthPlan();
                break;
            case R.id.tv_scan_shlc: //查看审核流程
                SgrjhCkshjlActivity.detailBean = mDetailBean;
                Intent intent = new Intent(this, SgrjhCkshjlActivity.class);
                startActivity(intent);
                break;
            case R.id.zf: //审核已完成时的作废功能
                mCenterDialog = new CenterDialog(this, new CenterDialog.CenterClick() {
                    @Override
                    public void confirmListener() {
                        postZuoFei();
                    }

                    @Override
                    public void cancelListener() {
                        mCenterDialog.dismiss();
                    }
                });
                mCenterDialog.setMessage("您确定要作废该计划吗?");
                mCenterDialog.show();
                break;
            case R.id.leftTextView:
                finish();
                break;
            default:
                break;
        }
    }

    //查看月计划功能  根据日计划对应月计划编号查询月计划详情
    private void readMonthPlan() {
        MyApplication.ShowWaitDialog(this);
        RequestParams mParams = new RequestParams();

        Map<String, String> map = new HashMap<>();
        map.put("bh", mDetailBean.getInfo().getYdbh());

        NetWorkApi.getZsyjhXx(map, new Callback<ShyjhxqBean>() {
                    @Override
                    public void onResponse(Call<ShyjhxqBean> call, Response<ShyjhxqBean> response) {
                        try {
                            ShyjhxqBean mBean = response.body();
                            if (mBean.getMsg().equals("success")) {
                                JhshReadActivity.ZsyjhxxShBean = mBean;
                                startActivity(new Intent(SgrjhYwcXqActivity.this, JhshReadActivity.class));
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<ShyjhxqBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //提交审核已完成作废功能
    private void postZuoFei() {
        Map<String, String> map = new HashMap<>();
        map.put("bh", mDetailBean.getInfo().getBh()); //周计划号
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.requestSgrjhywcZfData(map, new Callback<SuccessBean>() {
                    @Override
                    public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                        try {
                            SuccessBean mBean = response.body();

                            if (mBean.getMsg().equals("success")) {
                                MyToast.showToast("作废成功");
                                mCenterDialog.dismiss();
                                RefreshManager.Sgrjh.RefreshShrjh();
                                RefreshManager.Sgrjh.RefreshShywc();
                                RefreshManager.Sgrjh.RefreshRjhcx();
                                startActivity(new Intent(SgrjhYwcXqActivity.this, SgrjhActivity.class));
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SuccessBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }

        );
    }


    //请求查询日计划详情页面的数据
    private void requestDetailData() {
        Map<String, String> map = new HashMap<>();
        map.put("bh", bh);
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.getChaxunrjhDetailData(map, new Callback<SgcxrjhDetailBean>() {
                    @Override
                    public void onResponse(Call<SgcxrjhDetailBean> call, Response<SgcxrjhDetailBean> response) {
                        try {
                            mDetailBean = response.body();
                            if (mDetailBean.getMsg().equals("success")) {
                                if (mDetailBean.getInfo() != null) { //有数据，渲染页面
                                    lb.setText(mDetailBean.getInfo().getLb());
                                    rjhh.setText(mDetailBean.getInfo().getBh());
                                    ydbh.setText(mDetailBean.getInfo().getYdbh());
                                    kssj.setText(mDetailBean.getInfo().getExsgrq());
                                    xm.setText(mDetailBean.getInfo().getXlxbList().get(0).getXlmc());
                                    dd.setText(mDetailBean.getInfo().getXlxbList().get(0).getSgdd());
                                    hb.setText(mDetailBean.getInfo().getXlxbList().get(0).getXbmc());
                                    lc.setText(mDetailBean.getInfo().getXlxbList().get(0).getLic());
                                    xmmc.setText(mDetailBean.getInfo().getXmmc());
                                    sgnr.setText(mDetailBean.getInfo().getNrmc());
                                    sgjx.setText(mDetailBean.getInfo().getJxmc());
                                    if (mDetailBean.getInfo().getLb().equals("C")) {
                                        blfh_layout.setVisibility(View.GONE);
                                    } else {
                                        blfh_layout.setVisibility(View.VISIBLE);
                                        blfh.setText(mDetailBean.getInfo().getBlfhmc());
                                    }
                                    aqfx.setText(mDetailBean.getInfo().getAqfxmc());
                                    fxcs.setText(mDetailBean.getInfo().getAqfxcsmc());

                                    sgdw.setText(mDetailBean.getInfo().getSgdw());
                                    sgdwfzr.setText(mDetailBean.getInfo().getSgdwfzr());
                                    jldw.setText(mDetailBean.getInfo().getJldw());
                                    jldwfzr.setText(mDetailBean.getInfo().getJldwfzr());
                                    jsdw.setText(mDetailBean.getInfo().getJsdw());
                                    cwzd.setText(mDetailBean.getInfo().getCwzd());
                                    ddt.setText(mDetailBean.getInfo().getDdt());
                                    dxjz.setText(mDetailBean.getInfo().getSgdxjz());
                                    sbgldw.setText(mDetailBean.getInfo().getSbgldw());
                                    qtdw.setText(mDetailBean.getInfo().getQtdw());
                                    shcs.setText(mDetailBean.getInfo().getShcs());
                                    xmzgbm.setText(mDetailBean.getInfo().getXmzgbm());

                                } else {
                                    Toast.makeText(SgrjhYwcXqActivity.this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                MyToast.showNullObjectError(SgrjhYwcXqActivity.this);
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SgcxrjhDetailBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }

        );
    }
}
